<template>
  <div v-for="(v, k) in sessionTaskList" :key="k" class="task-progress">

    <div v-if="get_percent_type(v.session_percent)==='str'">
      <span>{{ v.session_name }}：{{ v.session_percent }}</span>
      <el-progress
          :percentage="100"
          :format="git_status_format"
          :status="get_status(v.session_status)"
          :indeterminate="v.session_status==='running'"
          :duration="5"
          :stroke-width="20"
      >
      </el-progress>
    </div>
    <div v-if="get_percent_type(v.session_percent)==='num'">
      <span>{{ v.session_name }}：{{ v.session_percent }}%</span>
      <el-progress
          :percentage="get_percent_num(v.session_percent)"
          :status="get_status(v.session_status)"
          :stroke-width="20"
      >
        <el-button type="danger" v-if="v.session_status==='running'" text>取消</el-button>
      </el-progress>
    </div>
  </div>


</template>

<script>
import axios from 'axios'

export default {
  name: "sessionTaskManage",
  props: {
    sessionToken: String
  },
  data() {
    return {
      sessionTaskList: [],
    }
  },
  methods: {
    get_status(status) {
      if (status === "waiting") {
        return "warning"
      } else if (status === "stop") {
        return "success"
      } else if (status === "fail") {
        return "exception"
      }
    },
    git_status_format() {
      return '拉取中'
    },
    get_percent_num(percent) {
      return parseInt(percent)
    },
    get_percent_type(percent) {
      let reg = /^\d+$/g;
      let r = percent.toString().match(reg);
      if (r != null && 0 <= this.get_percent_num(percent) <= 100)
        return "num"
      return "str"
    },
    get_session_task_by_session_token() {
      let obj_this = this
      let data = {"session_token": obj_this.sessionToken}
      axios.post('/get_session_task_by_session_token', data).then(function (response) {
        obj_this.sessionTaskList = response.data.data
      }).catch(function (error) { // 请求失败处理
        console.log(error);
      });
    }
  },
  mounted() {
    let obj_this = this
    setInterval(() => {
      obj_this.get_session_task_by_session_token()
    }, 1000)
  },
}
</script>

<style scoped>
.task-progress .el-progress--line {
  margin-bottom: 5px;
}
</style>